
#ifndef __XATOMIC_CHENXP__H_
#define	__XATOMIC_CHENXP__H_

#if defined( linux )
//#include <asm/atomic.h>
typedef struct
{
	volatile int counter;
} atomic_t;

#define atomic_read(x)					((x)->counter)
#define atomic_set(x, val)				((x)->counter = (val))
#define atomic_inc(x)					((void) __sync_fetch_and_add (&(x)->counter, 1))
#define atomic_dec_and_test(x)			(__sync_fetch_and_add (&(x)->counter, -1) == 1)
#define atomic_add(x, v)				((void) __sync_add_and_fetch(&(x)->counter, (v)))
#define atomic_dec(x, v)				((void) __sync_sub_and_fetch(&(x)->counter, (v)))
#define atomic_cmpxchg(x, oldv, newv)	__sync_val_compare_and_swap (&(x)->counter, oldv, newv) 
#define atomic_sub(x,v)					atomic_dec(x,v)
#endif	/*linux*/

#if defined( WIN32 )
#include <windows.h>

typedef struct
{
	volatile long	counter;
} atomic_t;

#define		ATOMIC_INIT(i)			{ (i) }

#define		atomic_read(v)			(v)->counter
#define		atomic_set(v,i)			(v)->counter = (i)

#define		atomic_inc(v)			InterlockedIncrement( &(v)->counter )
#define		atomic_dec(v)			InterlockedDecrement( &(v)->counter )
#define		atomic_add(i,v)			InterlockedExchangeAdd ( &(v)->counter, (i) )
#define		atomic_sub(i,v)			InterlockedExchangeAdd ( &(v)->counter, -(i) )

#define		atomic_inc_return(v)	InterlockedIncrement( &(v)->counter )
#define		atomic_dec_return(v)	InterlockedDecrement( &(v)->counter )
#define		atomic_add_return(i,v)	InterlockedExchangeAdd ( &(v)->counter, (i) )
#define		atomic_sub_return(i,v)	InterlockedExchangeAdd ( &(v)->counter, -(i) )

#define		atomic_xchg(v,n)		InterlockedExchange ( &(v)->counter, (n) )
#define		atomic_cmpxchg(v,o,n)	InterlockedCompareExchange( &(v)->counter, (n), (o) )

#endif	/*WIN32*/

#endif	/*__XATOMIC_CHENXP__H_*/


